/*
	This program is licensed under the GNU GPL v2 (or at your preference any later version)
	http://www.gnu.org/licenses/gpl-2.0.txt
*/

var mousedown=0;

var downX=0;
var downY=0;

var cobj = 1;
var sobj = 0;
var subobj=-1;
var objects = new Array();

var create="none";

function down(obj, e)
{
	sobj=obj;
	downX= e.clientX;
	downY= e.clientY;
	mousedown=1;
}

function up(e)
{
	mousedown=0;
}

function over(id, subid)
{
	subobj=subid;
	redraw();
}

function out()
{
	subobj=-1;
	redraw();
}

function move(e)
{
	if(mousedown)
	{
		objects[sobj].x += (e.clientX - downX);
		downX = e.clientX;
		objects[sobj].y += (e.clientY - downY);
		downY = e.clientY;
		redraw();
	}
}

function redraw()
{
	var svg = '<svg xmlns="http://www.w3.org/2000/svg" width="1920" height="1080">';
	var c=1;
	while(c < cobj)
	{
		svg += '<path ';
		if(c == sobj)
		{
			svg += 'style="' + "fill:none;stroke:#00ff00;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1" + '" ';
		}
		else
		{
			svg += 'style="' + objects[c].style + '" ';
		}
		svg += 'id="path' + objects[c].id + '" d="';
		var c2=0;
		while(c2 < objects[c].dc)
		{
			svg += objects[c].d[c2].compile();
			c2++;
		}
		svg += '" />';
		c2=0;
		while(c2 < objects[c].dc)
		{
			//svg += objects[c].d[c2].compile();
			if(c2 == subobj)
			{
				svg += '<rect x="' + (objects[c].d[c2].x-3) + '" y="' + (objects[c].d[c2].y-3) + '" width="6" height="6" fill="red" stroke="grey" stroke-width="1" onmouseout="out();" />';
			}
			else
			{
				svg += '<rect x="' + (objects[c].d[c2].x-3) + '" y="' + (objects[c].d[c2].y-3) + '" width="6" height="6" fill="none" stroke="grey" stroke-width="1" onmouseover="over(' + c + ', ' + c2 + ');" />';
			}
			c2++;
		}
		c++;
	}
	svg += '</svg>';

	//alert(svg);
	document.getElementById('graphic').innerHTML = svg;
}

function svgPath(id)
{
	this.id=id;
	this.style="fill:none;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"; //new svgStyle();
	this.d=new Array();
	this.dc=0;
}

function svgStyle()
{
	this.style = "fill:none;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1";
}

function svgPathMoveTo(x, y)
{
	this.x=x;
	this.y=y;
	this.compile = svgPathMoveToCompile;
}

function svgPathMoveToCompile()
{
	return 'M ' + this.x + ' ' + this.y;
}

function svgPathLineTo(x, y)
{
	this.x=x;
	this.y=y;
	this.compile = svgPathLineToCompile;
}

function svgPathLineToCompile()
{
	return 'L ' + this.x + ' ' + this.y;
}

function svgPathCurve(x1, y1, x2, y2, x, y)
{
	this.x1=x1;
	this.y1=y1;
	this.x2=x2;
	this.y2=y2;
	this.x=x;
	this.y=y;
	this.compile = svgPathCurveCompile;
}

function svgPathCurveCompile()
{
	return 'c ' + this.x1 + ' ' + this.y1 + ' ' + this.x2 + ' ' + this.y2 + ' ' + + this.x + ' ' + this.y;
}

function svgPathClose()
{
	return "z";
}

function newPath()
{
	create="path";
}

function click(t, e)
{
	if(sobj==0)
	{
		objects[cobj]= new svgPath
		(
			cobj
		);
		sobj=cobj;
		objects[sobj].d[objects[sobj].dc++]=new svgPathMoveTo(
			((e.clientX+t.scrollLeft)-40),
			((e.clientY+t.scrollTop)-120)
		);
		cobj++;
	}
	else
	{
		objects[sobj].d[objects[sobj].dc++]=new svgPathLineTo(
			((e.clientX+t.scrollLeft)-40),
			((e.clientY+t.scrollTop)-120)
		);
	}
	redraw();
}

function svgObj(x,y)
{
	this.x=x;
	this.y=y;
}

function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent)
	{
		do
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
		while (obj = obj.offsetParent);
		return [curleft,curtop];
	}
}
